package 第六周;

/**
 * @author xhw
 */

public class 外观数列 {

    public static void main(String[] args) {
        int n = 4;
        String result = countAndSay(n);
        System.out.println(result); // 输出: "1211"
    }

    public static String countAndSay(int n) {
        String str = "1";
        for (int i = 2; i <= n; i++) {
            StringBuilder builder = new StringBuilder();
            char c = str.charAt(0);
            int count = 1;
            for (int j = 1; j < str.length(); j++) {
                if (str.charAt(j) == c) {
                    count++;
                } else {
                    builder.append(count).append(c);
                    c = str.charAt(j);
                    count = 1;
                }
            }
            builder.append(count).append(c);
            str = builder.toString();
        }
        return str;
    }
}